import { Message, MessageBox } from 'element-ui'
import Vue from 'vue'
/**
 * @param msg
 * @param type
 */
const showStatusMsg = function (msg = '请指明操作！', type = 'error') {
  Message({
    message: msg,
    type: type
  })
}
/**
 * 普通消息
 */
const showMsg = function (msg = '提示消息') {
  Message(msg)
}
const showWarnMsg = function (msg = '警告消息') {
  showStatusMsg(msg, 'warn')
}
/***
 * 操作成功
 * @param msg
 * @param type
 */
const showSuccessMsg = function (msg = '操作成功', type = 'success') {
  showStatusMsg(msg, type)
}
/**
 * 操作失败
 */
const showFailMsg = function (msg = '操作失败', type = 'error') {
  showStatusMsg(msg, type)
}
/***
 * 请求后台数据失败
 * @param msg
 * @param error
 */
const reqFailMsg = function (msg = '操作失败！', error) {
  if (error) {
    msg = error && error.msg ? error.msg : msg
    showFailMsg(msg)
  }
}
const getPrefix = function (_this) {
  return _this ? _this.utilHelper.apiUrl : sessionStorage.prefixUrl
}

const getQueryString = function (name) {
  var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i')
  var r = window.location.search.substr(1).match(reg)
  if (r != null) {
    return unescape(r[2])
  }
  return null
}
const showCustomerInfo = function () {
  var includeCompanyUuids = [
    '2b53ec3f-9c83-11e8-b93d-000c2979eb04', // 测试环境公司
    '4e350cdb-4ee4-4494-80e1-de2a677dfda1', // 生产: 三合
    '9e4e724b-b109-11e8-89a5-005056889b83' // 生产: 巧房演示
  ]
  var companyUuid = getQueryString('companyUuid');
  return includeCompanyUuids.indexOf(companyUuid) >= 0
}

export default {
  showMsg,
  showWarnMsg,
  showStatusMsg,
  showSuccessMsg,
  showFailMsg,
  reqFailMsg,
  Log (logInfo, flag = true) {
    if (flag) {
      window.console.log(logInfo)
    }
  },
  /**
   * 确认框
   * @param options
   * @returns {Promise}
   */
  Confirm (options = {}) {
    options.tips = options.tips || '此操作将永久删除, 是否继续?'
    options.title = options.title || '提示'
    options.confirmBtn = options.confirmBtn || '确定'
    options.cancelBtn = options.cancelBtn || '取消'
    options.type = options.type || 'warning'
    return new Promise((resolve, reject) => {
      MessageBox.confirm(options.tips, options.title, {
        confirmButtonText: options.confirmBtn,
        cancelButtonText: options.cancelBtn,
        type: options.type
      }).then((rsp) => {
        resolve(rsp)
      }).catch(() => {
        Message({
          type: 'info',
          message: '已取消'
        })
      })
    })
  },
  getPrefix,
  getQueryString,
  showCustomerInfo,
  deepClone(obj) {
    return JSON.parse(JSON.stringify(obj))
   // return obj
  },
  stringfiyParams(params = {}, prefix = '') {
   // if(params instanceof 'object') {
      let query = '', keyList = Object.keys(params || {})
      for(let i = 0; i < keyList.length; i++ ) {
        let key = keyList[i]
        let val =params[key]
        if(val) {
          query += `${prefix}${key}=${val}&`
        }
      }
      return query
    //}

  }
}
